Solving 10385 - Duathlon (Ternary search)
[andmenj-acm.git] / 12324 - Philip J. Fry Problem / 12324.cpp
blobedfb5dfd4910eee39fecbee7c2bda5c0a44cd633
1 // Equipo Poncho, Carriel y Ruana
2 using namespace std;
3 #include <algorithm>
4 #include <iostream>
5 #include <iterator>
6 #include <sstream>
7 #include <fstream>
8 #include <cassert>
9 #include <climits>
10 #include <cstdlib>
11 #include <cstring>
12 #include <string>
13 #include <cstdio>
14 #include <vector>
15 #include <cmath>
16 #include <queue>
17 #include <stack>
18 #include <list>
19 #include <map>
20 #include <set>
22 template <class T> string toStr(const T &x)
23 { stringstream s; s << x; return s.str(); }
24 template <class T> int toInt(const T &x)
25 { stringstream s; s << x; int r; s >> r; return r; }
27 #define For(i, a, b) for (int i=(a); i<(b); ++i)
28 #define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x)
29 #define D(x) cout << #x " = " << (x) << endl;
31 const double EPS = 1e-9;
33 int cmp(double x, double y = 0, double tol = EPS) {
34 return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
37 const int MAXN = 110;
38 const int MAXB = MAXN * MAXN;
39 int dp[MAXN][MAXB];
41 int time_spent[MAXN];
42 int balls[MAXN];
44 int main(){
45 int n;
46 while (cin >> n) {
47 if (n == 0) break;
48 int totalBalls = 0;
49 For(i, 0, n) {
50 cin >> time_spent[i] >> balls[i];
51 totalBalls += balls[i];
53 for (int b = 0; b <= totalBalls; ++b) dp[n][b] = 0;
54 for (int i = n - 1; i >= 0; --i) {
55 for (int b = 0; b <= totalBalls; ++b) {
56 dp[i][b] = dp[i + 1][b + balls[i]] + time_spent[i];
57 if (b > 0) {
58 int option = dp[i + 1][b - 1 + balls[i]] + time_spent[i] / 2;
59 if (option < dp[i][b]){
60 dp[i][b] = option;
65 cout << dp[0][0] << endl;
67 return 0;